using System;
using System.Xml.Serialization;

namespace SharpMud.MudLib.SAMPLE.Commands
{
	[XmlRoot("CommandManagerConfiguration")]
	public class CommandManagerConfiguration
	{
		#region Private Variables
		private System.Collections.ArrayList	_CommandTypes;
		private bool							_IncludeDebugInformation;
		#endregion
		
		#region Constructors
		public CommandManagerConfiguration()
		{
			_CommandTypes = new System.Collections.ArrayList();
		}
		#endregion

		#region Public Properties
		public CommandTypeDefinition[] CommandTypes
		{
			get
			{
				return (CommandTypeDefinition[])_CommandTypes.ToArray(typeof(CommandTypeDefinition));;
			}
			set
			{
				_CommandTypes.AddRange(value);
			}
		}

		[XmlElement("IncludeDebugInformation")]
		public bool IncludeDebugInformation
		{
			get
			{
				return _IncludeDebugInformation;
			}
			set
			{
				_IncludeDebugInformation = value;
			}
		}
		#endregion

		#region Static Default
		public static CommandManagerConfiguration Default
		{
			get
			{
				CommandManagerConfiguration cmc = new CommandManagerConfiguration();
				
				cmc.IncludeDebugInformation = true;	

				CommandTypeDefinition ctd;
				ctd = new CommandTypeDefinition(
					".cmd.cs",
					typeof(Microsoft.CSharp.CSharpCodeProvider),
					DefaultAssemblyReferences );
				cmc._CommandTypes.Add(ctd);

				ctd = new CommandTypeDefinition(
					".cmd.vb",
					typeof(Microsoft.VisualBasic.VBCodeProvider),
					DefaultAssemblyReferences );
				cmc._CommandTypes.Add(ctd);

				return cmc;
			}
		}

				public static string[] DefaultAssemblyReferences
				{
					get
					{
						System.Collections.ArrayList al = new System.Collections.ArrayList();
						
						al.Add("mscorlib.dll");
						al.Add("System.dll");
						al.Add("System.Xml.dll");
                        al.Add("log4net.dll");
                        al.Add("SharpMud.Core.dll");
                        al.Add("SharpMud.Net.Telnet.dll");
                        al.Add("SharpMud.Server.App.Core.dll");
                        al.Add("SharpMud.Server.Systems.Core.dll");
                        al.Add("SharpMud.Server.Systems.Net.Core.dll");
                        al.Add("SharpMud.Server.Systems.Net.Telnet.dll");
                        al.Add("SharpMud.Terminal.Core.dll");
						
						System.Reflection.Assembly ass;
		
						//This attempts to add in every reasobable assembly that a command should
						//need to use.
						//It may prove to be excessive, or, it may prove that a more recursive search
						//should be done.
		
						ass = System.Reflection.Assembly.GetExecutingAssembly();
						al.Add(ass.GetName().CodeBase);
		
						ass = System.Reflection.Assembly.GetEntryAssembly();
						al.Add(ass.GetName().CodeBase);
		
						ass = System.Reflection.Assembly.GetCallingAssembly();
						al.Add(ass.GetName().CodeBase);
		
						for(int i=0;i<al.Count;i++)
						{
							if(((string)al[i]).ToLower().StartsWith("file:///"))
							{
								al[i] = (((string)al[i]).Substring(8,((string)al[i]).Length-8)).Replace("/",@"\");
							} 
							else if(((string)al[i]).ToLower().StartsWith("file://"))
							{
								al[i] = (((string)al[i]).Substring(7,((string)al[i]).Length-7)).Replace("/",@"\");
							}
						}
		
						//Return a string array of the references to be used
						return (string[])al.ToArray(typeof(string));
					}
				}
		#endregion
	}
}
